<?php

namespace app\api\model\service;

use think\Db;
use think\Model;
class UserInfo extends Model
{
    // 表名
    protected $name = 'service_user_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function getInfo($uid)
    {
        return self::where(['user_id'=>$uid])->field('user_id,mobile,is_skill,is_shop,alipay_name,alipay_account,money,shop_money,shop_user_money,is_update,bank_name,bank_user,bank_card,is_plus,discount,plusname,plustime')->find();
    }

    public static function getUserMoney($uid)
    {
        $userInfo = self::where(['user_id'=>$uid])->field('id,user_id,mobile,is_skill,is_shop,alipay_name,alipay_account,money,shop_money,shop_user_money,bank_name,bank_user,bank_card')->find();
        $userInfo['skillEnsurePrice'] = $userInfo['is_skill'] == 1?Skill::where(['user_id'=>$uid])->value('ensure_price'):0;
        $userInfo['shopEnsurePrice'] = $userInfo['is_shop'] == 1?Shop::where(['user_id'=>$uid])->value('ensure_price'):0;
        return $userInfo;
    }

    public static function getOpenid($id,$type)
    {
        $openid = '';
        switch ($type)
        {
            case 0:
                $openid = self::where('user_id',$id)->value('user_openid');
                break;
            case 1:
                $skillUserId = Skill::where('id',$id)->value('user_id');
                $openid = self::where('user_id',$skillUserId)->value('skill_openid');
                break;
            case 2:
                $shopUserId = Shop::where('id',$id)->value('user_id');
                $openid = self::where('user_id',$shopUserId)->value('shop_openid');
                break;
        }
        return $openid;
    }


    public static function getMobile($uid)
    {
        return self::where(['user_id'=>$uid])->value('mobile');
    }

    public static function money($money, $user_id, $memo,$order_id='')
    {
        Db::startTrans();
        try {
            $user = self::lock(true)->find($user_id);
            if ($user && $money != 0) {
                $before = $user->money;
                //$after = $user->money + $money;
                $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
                //更新会员信息
                $user->save(['money' => $after]);
                //写入日志
                UserMoneyLog::create(['user_id' => $user_id, 'money' => $money, 'order_id'=>$order_id, 'memo' => $memo]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }
    
    public static function bonus($bonus, $user_id, $memo,$order_id='')
    {
        
        Db::startTrans();
        try {
            $user = User::where(['id'=>$user_id])->lock(true)->find();
            if ($user && $bonus != 0) {
                $before = $user->bonus;
                //$after = $user->bonus + $bonus;
                $after = function_exists('bcadd') ? bcadd($user->bonus, $bonus, 2) : $user->bonus + $bonus;
                //更新会员信息
                $user->save(['bonus' => $after]);
                
                //写入日志
                $res = UserBonusLog::create(['user_id' => $user_id, 'money' => $bonus, 'before' => $before, 'after' => $after, 'order_id'=>$order_id, 'memo' => $memo]);
                // return $res;
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return $e->getMessage();
        }
        return true;
    }
    
    public static function skillBonus($bonus, $user_id, $memo,$order_id='')
    {
        Db::startTrans();
        try {
            $skill = Skill::where(['user_id'=>$user_id])->lock(true)->find();
            if ($skill && $bonus != 0) {
                $before = $skill->rebate_price;
                //$after = $user->bonus + $bonus;
                $after = function_exists('bcadd') ? bcadd($skill->rebate_price, $bonus, 2) : $skill->rebate_price + $bonus;
                //更新会员信息
                $skill->save(['rebate_price' => $after]);
                //写入日志
                SkillBonusLog::create(['user_id' => $user_id, 'money' => $bonus, 'before' => $before, 'after' => $after, 'order_id'=>$order_id, 'memo' => $memo]);
            }
            Db::commit();
            
        } catch (\Exception $e) {
            Db::rollback();
        }
        
    }

    public static function shopUserMoney($money, $user_id, $memo,$order_id='')
    {
        Db::startTrans();
        try {
            $user = self::where(['user_id'=>$user_id])->lock(true)->find();
            if ($user && $money != 0) {
                $before = $user->shop_user_money;
                //$after = $user->money + $money;
                $after = function_exists('bcadd') ? bcadd($user->shop_user_money, $money, 2) : $user->shop_user_money + $money;
                //更新会员信息
                $user->save(['shop_user_money' => $after]);
                //写入日志
                ShopUserMoneyLog::create(['user_id' => $user_id, 'money' => $money, 'order_id'=>$order_id, 'memo' => $memo]);

            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }


    public static function shopMoney($money, $user_id, $memo,$order_id='')
    {
        Db::startTrans();
        try {
            $user = self::where(['user_id'=>$user_id])->lock(true)->find();
            if ($user && $money != 0) {
                $before = $user->money;
                //$after = $user->money + $money;
                $after = function_exists('bcadd') ? bcadd($user->shop_money, $money, 2) : $user->shop_money + $money;
                //更新会员信息
                $user->save(['shop_money' => $after]);
                //写入日志
                ShopMoneyLog::create(['user_id' => $user_id, 'money' => $money, 'order_id'=>$order_id, 'memo' => $memo]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }







}